package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.Equipment;
import com.mt.roll.core.dal.service.EquipmentServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.EquipmentVoMapper;
import com.mt.roll.wms.params.EquipmentParam;
import com.mt.roll.wms.params.EquipmentQueryParam;
import com.mt.roll.wms.service.EquipmentService;
import com.mt.roll.wms.vo.EquipmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther yanyang
 * @date 2021/11/01 14:16
 */

@Service
@Transactional
public class EquipmentServiceImpl extends BaseService implements EquipmentService {

    @Autowired
    private EquipmentServiceBiz equipmentServiceBiz;

    @Autowired
    private EquipmentVoMapper equipmentVoMapper;

    @Override
    public R<PageVo<EquipmentVo>> page(EquipmentQueryParam equipmentQueryParam) {
        IPage<EquipmentVo> page = equipmentVoMapper.page(new Page<>(equipmentQueryParam.getCurrent(),equipmentQueryParam.getSize()),equipmentQueryParam);
        return successful(new PageVo<>(page,EquipmentVo.class));
    }

    @Override
    public R<EquipmentVo> get(IdParam idParam) {
        Equipment equipment = equipmentServiceBiz.getById(idParam.getId());
        Assert.notNull("详情不存在", equipment);
        EquipmentVo equipmentVo = equipmentVoMapper.getById(idParam.getId());
        return successful(equipmentVo);
    }

    @Override
    public R<IdVo> add(EquipmentParam equipmentParam){
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq(Equipment.CODE, equipmentParam.getCode());
        Assert.eqZero(equipmentServiceBiz.count(wrapper), "该设备已经存在");
        Equipment equipment = new Equipment();
        BeanUtils.copyProperties(equipmentParam, equipment);
        setCommonField(equipment);
        equipmentServiceBiz.save(equipment);
        return successful(IdVo.builder().id(equipment.getId()).build());
    }

    @Override
    public R<IdVo> update(EquipmentParam equipmentParam) {
        Equipment equipment = equipmentServiceBiz.getById(equipmentParam.getId());
        if (!equipment.getCode().equals(equipmentParam.getCode())) {
            QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
            wrapper.eq(Equipment.CODE, equipmentParam.getCode());
            Assert.eqZero(equipmentServiceBiz.count(wrapper), "该设备已经存在");
        }
        Equipment updateAlarm = new Equipment();
        BeanUtils.copyProperties(equipmentParam, updateAlarm);
        setUpdateCommonField(updateAlarm);
        equipmentServiceBiz.updateById(updateAlarm);
        return successful(IdVo.builder().id(equipment.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        Equipment equipment = equipmentServiceBiz.getById(idParam.getId());
        if (equipment == null) {
            return failed("设备信息不存在");
        }
        if (equipment.getEnabled()==1){
            return failed("正常运行的设备不可以删除");
        }
        Equipment updateAlarm = new Equipment().setId(equipment.getId());
        updateAlarm.setValid(WhetherEnum.NO.getValue());
        setUpdateCommonField(updateAlarm);
        equipmentServiceBiz.updateById(updateAlarm);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        equipmentServiceBiz.removeById(updateAlarm.getId());
        //equipmentServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(equipment.getId()).build());
    }

    @Override
    public R<String> getCode() {
        return successful(CodeGeneratorHelper.getEquipmentCode());
    }
}
